package com.graduate.cec.service;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graduate.cec.common.JsonPage;
import com.graduate.cec.common.ResponseCode;
import com.graduate.cec.exception.CecServiceException;
import com.graduate.cec.mapper.AdminMapper;
import com.graduate.cec.mapper.PermissionMapper;
import com.graduate.cec.mapper.RoleMapper;
import com.graduate.cec.mapper.RolePermissionMapper;
import com.graduate.cec.pojo.dto.AdminSimplePageDTO;
import com.graduate.cec.pojo.dto.RoleSimplePageDTO;
import com.graduate.cec.pojo.entity.*;
import com.graduate.cec.pojo.vo.AdminSimpleVO;
import com.graduate.cec.pojo.vo.AdminVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class RoleService {
    @Resource
    RoleMapper roleMapper;

    @Resource
    PermissionMapper permissionMapper;
    @Resource
    RolePermissionMapper rolePermissionMapper;

    public void insertRole(Role role) {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andNameEqualTo(role.getName());
        List<Role> roles = roleMapper.selectByExample(roleExample);
        if(roles.size() != 0){
            throw new CecServiceException(ResponseCode.INSERT_INFO_ERROR, "角色已存在!");
        }else{
            roleMapper.insertSelective(role);
        }
    }

    public void deleteRole(Long id) {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andIdEqualTo(id);
        int i = roleMapper.deleteByPrimaryKey(id);
        if(i == 0){
            throw new CecServiceException(ResponseCode.DELETE_INFO_ERROR, "删除失败!");
        }
    }






    public JsonPage<Role> selectAllRole(RoleSimplePageDTO roleSimplePageDTO) {
        PageHelper.startPage(roleSimplePageDTO.getPageNum(), roleSimplePageDTO.getPageSize());

        RoleExample roleExample = new RoleExample();
        List<Role> roles = roleMapper.selectByExample(roleExample);


        return JsonPage.restPage(new PageInfo<>(roles));

    }

    public List<Permission> selectAllPermission( ){

        List<Permission> permissions = permissionMapper.selectByExample(new PermissionExample());
        return permissions;
    }

    public List<Permission> selectAllPermissionByRole(Long id){
        List<Permission> permissions = permissionMapper.selectAllPermissionByRole(id);
        return permissions;
    }


    public void changePermission(Long[] value, Long roleId){
        RolePermissionExample rolePermissionExample = new RolePermissionExample();
        RolePermissionExample.Criteria criteria = rolePermissionExample.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        rolePermissionMapper.deleteByExample(rolePermissionExample);

        RolePermission rolePermission = new RolePermission();
        for (Long aLong : value) {
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(aLong);
            rolePermissionMapper.insertSelective(rolePermission);
        }


    }







}
